package cn.iocoder.yudao.module.system.tcpserver.utils;

public class ZkCrcUtils {
    public static byte[] getCrc(byte[] data) {
        int len = data.length - 2;
        int crc = 0xFFFF; // 初始化16位寄存器，所有位均为1
        for (int i = 0; i < len; i++) {
            crc = (crc & 0xFF00) | (crc & 0x00FF) ^ (data[i] & 0xFF);
            for (int j = 0; j < 8; j++) {
                if ((crc & 0x0001) > 0) {
                    crc = crc >> 1;
                    crc = crc ^ 0xA001; // 0xA001是预设的固定多项式
                } else {
                    crc = crc >> 1;
                }
            }
        }
        byte[] src = new byte[2];
        src[0] = (byte) ((crc >> 8) & 0xFF);
        src[1] = (byte) (crc & 0xFF);
        return src;
    }
}
